package com.zhoug.player3demo;


import android.content.Context;
import android.content.SharedPreferences;


import com.zhoug.common3.json.JsonUtils;
import com.zhoug.player3.core.CameraToken;

import androidx.annotation.NonNull;

/**
 * 和目数据保存
 *
 * @Author 35574
 * @Date 2020/12/10
 * @Description
 */
public class SharedPreferencesUtils {
    private static final String TAG = ">>>AndMuPrepareUtils";
    private static final String XML_NAME = "hekingQlyOpenSdk";
    private static final String KEY_QLY_OPEN_TOKEN = "hekingqlyToken";


    private static SharedPreferences getQlyOpenSP() {
      return   App.getApplication().getSharedPreferences(XML_NAME, Context.MODE_PRIVATE);
    }


    /**
     * 缓冲应用级token
     * 获取到的accessToken有效期是7天，请在即将过期或者接口报错11504时重新获取，
     * 请勿频繁调用，频繁调用将会被拉入限制黑名单
     * @param token
     */
    public static void setQlySdkToken(CameraToken token) {
        SharedPreferences sharedPreferences = getQlyOpenSP();
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putString(KEY_QLY_OPEN_TOKEN, token != null ? JsonUtils.toJson(token) : null);
        edit.apply();
    }

    /**
     * 获取应用级token
     *
     * @return
     */
    public static CameraToken getQlySdkToken() {
        String string = getQlyOpenSP().getString(KEY_QLY_OPEN_TOKEN, null);
        if (string != null) {
            return JsonUtils.fromJson(string, CameraToken.class);
        }
        return null;
    }

    /**
     * 缓冲摄像头token
     * @param monitorId
     * @param token
     */
    public static void setMonitorToken(@NonNull String monitorId, CameraToken token) {
        SharedPreferences sharedPreferences = getQlyOpenSP();
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putString(monitorId, token != null ? JsonUtils.toJson(token) : null);
        edit.apply();
    }

    /**
     * 获取摄像头token
     *
     * @return
     */
    public static CameraToken getMonitorToken(@NonNull String monitorId) {
        String string = getQlyOpenSP().getString(monitorId, null);
        if (string != null) {
            return JsonUtils.fromJson(string, CameraToken.class);
        }
        return null;
    }

}
